<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>三岁博客后台管理-公告管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="icon" href="../../static/images/favicon.ico" th:href="@{/images/favicon.ico}"/>
    <link rel="stylesheet" href="../../static/lib/admin/lib/layui-v2.6.3/css/layui.css" th:href="@{/lib/admin/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" href="../../static/lib/admin/css/public.css" th:href="@{/lib/admin/css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">公告标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="notitle" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="select">查看</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script><!--引入jquery js-->
<script src="../../static/lib/admin/lib/layui-v2.6.3/layui.js" th:src="@{/lib/admin/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script src="../../static/lib/admin/js/lay-config.js?v=1.0.4" th:src="@{/lib/admin/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script>


    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        //加载文章信息
        table.render({
            elem: '#currentTableId',
            url: '../page/getNoticeList',
            method: 'post',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                {type: "checkbox", width: 50},
                {type: 'numbers',title: '序号', width:80,sort:true},
                {field: 'noticeId', width: 80, title: 'ID'},
                {field: 'notitle', width: 200, title: '公告标题'},
                {field: 'createtime', title: '发布时间', minWidth: 170},
                {field: 'createuser', width: 80, title: '发布人'},
                {field: 'status', width: 135, title: '状态',
                    templet: function (d) {
                        var state = "";
                        if (d.status == "1") {
                            state = "<input type='checkbox' value='" + d.noticeId + "' id='status' lay-filter='stat' checked='checked' name='status'  lay-skin='switch' lay-text='生效|未生效' >";
                        }
                        else {
                            state = "<input type='checkbox' value='" + d.noticeId + "' id='status' lay-filter='stat'  name='status'  lay-skin='switch' lay-text='生效|未生效' >";

                        }
                        return state;
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            initSort: {
                field: 'createtime',
                type: 'desc'
            },
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });



        //监听开关事件
        form.on('switch(stat)', function (data) {
            var contexts;
            var sta;
            var x = data.elem.checked;//判断开关状态
            if (x==true) {
                contexts = "修改";
                status=1;
            } else {
                contexts = "失效";
                status=0;
            }
            //自定义弹窗
            layer.open({
                content: "你确定要"+contexts+"?"
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    //按钮确定【按钮一】的回调
                    data.elem.checked = x;
                    $.ajax({
                        type: "post",
                        url: '../page/notice/updateStatus',
                        data: {
                            "noticeId": data.value,
                            "status":status
                        },
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg(contexts+'成功',{icon: 1, time: 2000,});
                            }else{
                                layer.msg(data.msg,{icon: 5, time: 2000,});
                            }
                        }
                    });

                    form.render();
                    layer.close(index);

                }
                , btn2: function (index, layero) {
                    //按钮【按钮二】的回调
                    data.elem.checked = !x;
                    form.render();
                    layer.close(index);
                    //return false 开启该代码可禁止点击该按钮关闭
                }
                , cancel: function () {
                    //右上角关闭回调
                    data.elem.checked = !x;
                    form.render();
                    // return false; //开启该代码可禁止点击该按钮关闭
                }
            });

            return false;

        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });

            var notitle = data.field.notitle;

            if (notitle == ''){
                layer.msg("请输入查询内容！", {icon: 5});
            }else{
                //执行搜索重载
                table.reload('currentTableId', {
                    page: {curr: 1},
                    where: {notitle: notitle},
                    method: 'post',
                    url: '../page/notice/noticeSearch'
                });
            }

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新增公告',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: './notice/add',
                });

            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                // layer.alert(JSON.stringify(data));

                delList = '';
                data.forEach(function (n, i) {
                    delList += n.noticeId + ',';
                });
                if (delList != '') {
                    layer.confirm('确定删除所选项吗', function (index) {
                        $.ajax({
                            url: '../page/notice/deleteNoticeByIds',
                            type: 'post',
                            data: {'noticeIds': delList},
                            success: function (suc) {
                                if (suc.code == 200) {
                                    table.reload('currentTableId',{});
                                    layer.msg(suc.msg, {icon: 1});
                                } else {
                                    layer.msg(suc.msg, {icon: 5});
                                }
                            },
                            'error': function () {
                                layer.msg('系统错误');
                            }
                        });
                    });

                } else {
                    layer.msg("请选择要删除的行！", {icon: 5});
                }



            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            // console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            window.formData = obj.data;
            if (obj.event === 'select') {
                var index = layer.open({
                    title: '查看公告',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: './notice/check',
                });
                // $(window).on("resize", function () {
                //     layer.full(index);
                // });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的要删除么', function (index) {

                    $.ajax({
                        url: '../page/notice/deleteNotice',
                        type: 'post',
                        data: {'noticeId': data.noticeId},//向服务端发送删除的id
                        success: function (suc) {
                            if (suc.code == 200) {
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                                // console.log(index);
                                layer.msg(suc.msg, {icon: 1});
                            } else {
                                layer.msg(suc.msg, {icon: 5});
                            }
                        }
                    });

                    obj.del();
                    layer.close(index);
                });
            }
        });

    });




</script>

</body>
</html>